﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Fsb.Utility;
using VideoCache.Aggregation.Interfaces;

namespace VideoCache.Aggregation
{
    public class FetcherFactory
    {
        public static VideoModel GetToken(string url)
        {
            string host = UrlHelper.GetHost(url);

            Assembly assembly = Assembly.GetExecutingAssembly();

            foreach (Type type in assembly.GetTypes())
            {
                foreach (Type interfaceType in type.GetInterfaces())
                {
                    if (interfaceType == typeof(IFetcher))
                    {
                        foreach (var attribute in type.GetCustomAttributes(typeof(SiteAttribute), true))
                        {
                            if (host == ((SiteAttribute)attribute).Host)
                            {
                                IFetcher fetcher = Activator.CreateInstance(type) as IFetcher;

                                return fetcher.GetToken(url);
                            }
                        }
                    }
                }
            }

            return null;
        }
    }
}